define(function(require, exports, module) {
    "use strict";
    var $ = require("$");
    var tmp = require('./template/letter.html');
    var template = require("./template.js")
    var transMsgTime = function(a) {
        var b, c, d, e, f, g = {
            0: "周日",
            1: "周一",
            2: "周二",
            3: "周三",
            4: "周四"
        };
        return a ? (c = new Date, b = "object" == typeof a && a.constructor === Date ? a : new Date(a), d = (c - b) / 1e3, 60 > d ? "刚刚" : 3600 > d ? Math.floor(d / 60) + "分钟前" : 10800 > d ? Math.floor(d / 3600) + "小时前" : (f = {
            year: b.getFullYear(),
            month: b.getMonth() + 1,
            date: b.getDate(),
            day: b.getDay(),
            hours: b.getHours() >= 10 ? b.getHours() : "0" + b.getHours().toString(),
            min: b.getMinutes() >= 10 ? b.getMinutes() : "0" + b.getMinutes().toString()
        }, e = {
            year: c.getFullYear(),
            month: c.getMonth() + 1,
            date: c.getDate(),
            day: c.getDay()
        }, f.date === e.date && f.month === e.month && f.year === e.year ? "今天" + f.hours + ":" + f.min : e.date - f.date === 1 && 172800 > d ? "昨天" + f.hours + ":" + f.min : e.day - f.day > 0 && 604800 > d ? g[f.day] + f.hours + ":" + f.min : f.year === e.year ? f.month + "月" + f.date + "日" : f.year + "年" + f.month + "月" + f.date + "日")) : ""
    };
    template.helper('transMsgTime', function(date) {
        return transMsgTime(1e3 * parseInt(date));
    });
    function func(s){
        s.push(s.shift());
        document.title= s.join("");
    }
    var fun;
    function stopX(){  
        window.clearInterval(fun);  
    } 
    var msgfunshow=function(){
        $.ajax({
            type: 'POST',
            url: window.letter_path,
            success: function(data) {
                var html = template.compile(tmp)(data)+$("#js_msg_content_footer").html(); 
                $(".js_msg_content").html(html)
                var $title=$("title"),$msg=$(".js_show_msg"),def=$msg.data("default"),msg=$msg.data("msg");
                if (data.count) {
                    $title.html(msg+def);
                    $(".js_msg_count").fadeOut(0, function() {
                        $('.js_msg_count').html(data.count);
                        $(".js_msg_count").fadeIn(0);
                    });
                }else{
                    $(".js_msg_count").fadeOut(0);
                    stopX();
                    $title.html(def);
                };
                $(".js_msg_empty").toggle(data.items.length>0)
            },
            dataType: 'json'
        });
    }
    var msgfun = function() {
        $.ajax({
            type: 'POST',
            url: window.letter_path,
            success: function(data) {
                var html = template.compile(tmp)(data)+$("#js_msg_content_footer").html(); 
                $(".js_msg_content").html(html);
                var $title=$("title"),$msg=$(".js_show_msg"),def=$msg.data("default"),msg=$msg.data("msg");
                if (data.count) {
                    $title.html(msg+def);
                    $(".js_msg_count").fadeOut(1000, function() {
                        $('.js_msg_count').html(data.count);
                        $(".js_msg_count").fadeIn(1000);
                    });
                    var s = (msg+def).split("");
                    if(fun==undefined){
                        fun=setInterval(function(){func(s)}, 200)
                    }
                    
                }else{
                    $(".js_msg_count").fadeOut(0);
                    stopX();
                    $title.html(def);
                    
                };
                $(".js_msg_empty").toggle(data.items.length>0)
            },
            dataType: 'json'
        });

        setTimeout(msgfun, 60000);
    }
    $(document).on("click",".js_msg_empty", function() {
        var $this = $(this),
            path = $this.data("path");
        if (path) {
            $.ajax(path, {
                type: "post",
                dataType: "json"
            }).done(function(d) {
                if (d.status == 0) {
                    $(".js_msg_count,.js_msg_hdead").hide()
                    $(".js_msg_content_items").html('<div class="wrapper">您目前没有新消息</div>')
                } else {
                    // window.msg.info("网络异常请重试")
                }
            }).fail(function() {
                window.msg.info("网络异常请重试")
            });
        };

    })
    if (window.letter_path) {
       msgfun();
       $(".js_show_msg").on("hide.bs.dropdown",function(){
        setTimeout(msgfunshow,5000);
    });
    }
    


})
